from django import forms
from rest_framework import serializers

from .models import Course
from django.contrib.auth.models import User


class CourseForm(forms.ModelForm):
    class Meta:
        model = Course
        fields = ('name', 'introduction', 'teacher', 'price')


class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = '__all__'

class CourseSerializer(serializers.ModelSerializer):

    # 外键字段 只读
    teacher = serializers.ReadOnlyField(source='teacher.username')

    class Meta:
        model = Course
        # exclude = ('id',)
        fields = ('id', 'name', 'introduction', 'teacher', 'price')
        # fields = '__all__'
        # 深度
        depth = 2


# class CourseSerializer(serializers.HyperlinkedModelSerializer):
#
#     # 外键字段 只读
#     teacher = serializers.ReadOnlyField(source='teacher.username')
#
#     class Meta:
#         model = Course
#         # url 是默认值，可在settings.py中设置URL_FIELD_NAME使全局生效
#         fields = ('id', 'url', 'name', 'introduction', 'teacher', 'price')